<template>
  <div class="warp-box">
    <el-row class="search-box" :gutter="10">
      <el-col :span="20">
        <el-input
          style="width:340px"
          v-model="parames.providerName"
          size="small"
          clearable
          placeholder="搜索服务商名称"
        ></el-input>
        <el-button size="small" type="primary" icon="el-icon-search" @click="search">搜索</el-button>
        <el-button size="small" type="text" icon="el-icon-s-open" @click="handleSearch">高级搜索</el-button>
      </el-col>
    </el-row>
    <el-row class="main-box channel1234" style="margin:10px -5px;">
      <el-col :span="24" v-if="newBtn">
        <el-button size="small" type="primary" icon="el-icon-plus" @click="handleAdd">新建服务商</el-button>
      </el-col>
      <el-col :span="24">
        <el-table
          :data="tableData"
          border
          v-loading="loading"
          @row-click="handleRowH"
          :row-class-name="tableRowClassName"
          style="margin-top:14px;"
        >
          <el-table-column
            show-overflow-tooltip
            width="200"
            fixed="left"
            prop="providerName"
            label="服务商名称"
          ></el-table-column>
          <el-table-column show-overflow-tooltip  prop="shortName" label="简称"></el-table-column>
          <el-table-column prop="contactName" label="联系人"></el-table-column>
          <el-table-column prop="contactMobile" label="联系方式"></el-table-column>
          <el-table-column  prop="dengji" label="联系人角色">
            <template slot-scope='scope'>
              <p v-if="scope.row.contactRole">{{scope.row.contactRole.desc}}</p>
            </template>
          </el-table-column>
          <el-table-column width="80" align="center" label="操作" fixed="right">
            <template slot-scope="scope">
                <el-button v-if="editBtn" @click.stop="handleRow(scope.row)" size="mini" type="text" icon="el-icon-edit"></el-button>
                <!-- <el-button @click.stop="handleDelete(scope.row)" size="mini" type="text" icon="el-icon-delete"></el-button> -->
                <a-popconfirm
                  title="是否确认删除?"
                  @confirm="handleDelete(scope.row)"
                  okText="是"
                  cancelText="否"
                >
                  <el-button @click.stop="" size="mini" type="text" icon="el-icon-delete"></el-button>
                </a-popconfirm>
            </template>
          </el-table-column>
        </el-table>
      </el-col>
      <el-col :span="24">
        <pages :parames="parames" @render="checkPage"></pages>
      </el-col>
    </el-row>
    <createdDialog :dialogData="dialogData" @checkRender="getList"></createdDialog>
    <searchDialog :searchData="searchData" @checkSearch="checkSearch" @resetSearch="resetSearch"></searchDialog>
    <detailDialog :detailData="detailData" @checkRender="getList"></detailDialog>

  </div>
</template>

<script>
import createdDialog from "./createdServiceDialog.vue"; //新建服务商
import searchDialog from "./opportSearchDialog.vue"; //高级搜索弹窗
import detailDialog from "./detailDialog.vue"; //详情弹窗

export default {
  data() {
    return {
      loading: false,
      parames: {
        page: 1, //页码index
        limit: 10, //页码最大
        providerName: '', //联系人名称
        totalCount: 0,
        search: []
      },
      dialogData: {
        //新建弹窗
        type: '',
        title: '',
        show: false
      },
      searchData: {
        //搜索弹窗
        show: false
      },
      titleData: {
        //表头列弹窗
        show: false
      },
      detailData: {
        //详情弹窗
        show: false
      },
      tableData: [],
      newBtn: false,
      editBtn: false
    };
  },
  components: {
    createdDialog,
    searchDialog,
    detailDialog
  },
  methods: {
    search() {
      //查询
      this.parames.page = 1;
      let searchDate = this.parames.search
      let n = 0
      this.parames.search.forEach(el=>{
        if (el.key == 'providerName') {
          n = 1
        }
      })
      if (this.parames.providerName && n == 0) {
        this.parames.search.push({
          key: 'providerName',
          value: this.parames.providerName,
          type: 'string'
        })
      } else if (this.parames.providerName && n == 1) {
        this.parames.search.forEach((el,index)=>{
          if (el.key == 'providerName') {
            el.value = this.parames.providerName
          }
        })
      } else if (!this.parames.providerName && n == 1) {
        this.parames.search.forEach((el,index)=>{
          if (el.key == 'providerName') {
            this.parames.search.splice(index,1)
          }
        })
      }
      this.getList()
    },
    handleSearch() {
      //打开高级搜索弹窗
      this.searchData.providerName = this.parames.providerName
      this.searchData.show = true;
    },
    resetSearch () {
      this.parames.providerName = ''
    },
    checkSearch (value) {
      this.parames.search = value
      value.forEach(el=>{
        if(el.key == 'providerName') {
          this.parames.providerName = el.value
        }
      })
      this.parames.page = 1
      this.getList()
    },
    checkPage () {
      this.getList()
    },
    handleSetTitle () { //设置表头列
      this.titleData.show = true
    },
    handleAdd() {//新建
      this.dialogData.type = 'new';
      this.dialogData.title = '新建服务商';
      this.dialogData.show = true;
    },
    handleDelete (row) { //删除
      this.$post(this.$api.postProviderDel,{
        providerId: row.id
      }).then(response => {
        if (response.status == 200) {
        }
        setTimeout(() => {
          this.getList()
        }, 200);
      })
    },
    getList() {//获取页面数据
      this.loading = true
      this.$post(this.$api.postProviderList,this.parames).then(response => {
        if (response.data.succeed) {
          this.parames.totalCount = response.data.totalCount
          this.tableData = response.data.data
        }
        setTimeout(() => {
          this.loading = false
        }, 200);
      })
    },
    handleRowH (row) {
      
      this.detailData.show = true
      this.detailData.id = row.id
    },
    handleRow(row) {
      //点击某一行数据修改
      this.dialogData.type = 'edit'
      this.dialogData.title = '修改服务商';
      this.dialogData.id = row.id;
      this.dialogData.show = true;
    },
    tableRowClassName({ row, rowIndex }) {
      //斑马表格
      if (rowIndex % 2 === 1) {
        return "warning-row";
      } else {
        return "success-row";
      }
      return "";
    }
  },
  mounted() {
    let buttonLimit = this.$utils.getLocalStorage('buttonlist')  //按钮缓存
    if (buttonLimit.indexOf('k063') !== -1) {
      this.newBtn = true
    }
    if (buttonLimit.indexOf('k064') !== -1) {
      this.editBtn = true
    }
    this.getList()
  }
};
</script>

<style>
.el-table .warning-row {
  background: #f9f9f9;
}
.el-table .success-row {
  background: #fff;
}
</style>